package com.ljj.examisto.ui.component

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.SweepGradient
import android.util.AttributeSet
import android.view.View
import com.ljj.examisto.R
import kotlin.math.min

class RingProgressBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    companion object {
        // 尺寸常量
        private const val START_ANGLE = -90f     // 起始角度
        // 精度常量
        private const val MAX_PROGRESS = 100.0f   // 最大进度值
        private const val MAX_ANGLE = 360.0f      // 最大角度
        private const val MIN_DRAW_ANGLE = 0.01f  // 最小绘制角度
    }
    // 引用参数
    private val strokeWidth: Float = resources.getDimension(R.dimen.ring_progress_bar_width)    // 圆环宽度
    private val bgColor = context.getColor(R.color.background)
    private val progressStartColor = context.getColor(R.color.background)
    private val progressEndColor = context.getColor(R.color.bright_blue)
    // 绘制工具
    private val backgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        color = bgColor
        strokeWidth = this@RingProgressBar.strokeWidth
        strokeCap = Paint.Cap.BUTT
    }
    private val progressPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = this@RingProgressBar.strokeWidth
        strokeCap = Paint.Cap.BUTT
        isDither = true
    }
    // 状态变量
    private var currentProgress = 0.0f
    private var currentAngle = 0.0f

    // 设置进度（0-100f）
    fun setProgress(progress: Float) {
        val clampedProgress = progress.coerceIn(0.0f, MAX_PROGRESS)
        if (clampedProgress != currentProgress) {
            currentProgress = clampedProgress
            currentAngle = (clampedProgress * MAX_ANGLE) / MAX_PROGRESS
            invalidate()
        }
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val size = min(width, height).toFloat()
        val halfStroke = this@RingProgressBar.strokeWidth / 2.0f
        val radius = size / 2.0f - halfStroke
        val centerX = width / 2.0f
        val centerY = height / 2.0f
        val rectF = RectF(
            centerX - radius,
            centerY - radius,
            centerX + radius,
            centerY + radius
        )
        setupDynamicGradient(centerX, centerY)
        canvas.drawOval(rectF, backgroundPaint)
        if (currentAngle > MIN_DRAW_ANGLE) {
            canvas.drawArc(
                rectF,
                START_ANGLE + MIN_DRAW_ANGLE,
                currentAngle - MIN_DRAW_ANGLE,
                false,
                progressPaint
            )
        }
    }

    private fun setupDynamicGradient(centerX: Float, centerY: Float) {
        val gradientPositions = if (currentProgress > 0) {
            val progressRatio = currentAngle / MAX_ANGLE
            floatArrayOf(0f, progressRatio) // 压缩整个渐变范围
        } else {
            floatArrayOf(0f, 0f)
        }
        SweepGradient(
            centerX,
            centerY,
            intArrayOf(progressStartColor, progressEndColor),
            gradientPositions
        ).apply {
            Matrix().apply {
                setRotate(-90f, centerX, centerY)
                setLocalMatrix(this)
            }
            progressPaint.shader = this
        }
    }
}